//
//  RemindStockVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/8.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "RemindStockVC.h"
#import "XCMultiSortTableView.h"
#import "StockAddVC.h"
#import "StockedEditVC.h"
#import "MBTUiColor.h"
#import "StockCode.h"
#import "HttpTool.h"
#import "stockDetail.h"
#import "StockResult.h"
#import "StockData.h"
#import "DbManage.h"
#import "MBTPre.h"


@interface RemindStockVC ()<XCMultiTableViewDataSource,UIGestureRecognizerDelegate>{
    NSMutableArray *headData;
    NSMutableArray *rightTableData;
    DbManage *db;
    NSMutableArray *stockData;
    BOOL once;
    XCMultiTableView *tableView;
}
@property (weak, nonatomic) IBOutlet UIView *content;
@property (weak, nonatomic) IBOutlet UIView *add;
@property (weak, nonatomic) IBOutlet UIView *edit;
@end

@implementation RemindStockVC

- (void)viewDidLoad{//调整字体大小
    [super viewDidLoad];
    NSLog(@"高什么鬼啊的啊");
    once=YES;
    stockData =[NSMutableArray array];
    rightTableData = [NSMutableArray array];
    db = [DbManage shareDbManage];
    [self initData];
    tableView= [[XCMultiTableView alloc]initWithFrame:CGRectInset(self.view.bounds, 0.0f, 0.0f)];
    tableView.leftHeaderEnable = YES;
    tableView.datasource = self;
    [_content addSubview:tableView];
    [self addStock];
    [self editStock];
    //不明白为什么要延时执行真是无语
    [self performSelector:@selector(setStockData) withObject:nil afterDelay:2.0f];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:NO];
    [self performSelector:@selector(setStockData) withObject:nil afterDelay:0.0f];
}

-(void)setStockData{//
    if (stockData.count!=0) {
        [stockData removeAllObjects];
    }
    if (rightTableData.count!=0) {
        [rightTableData removeAllObjects];
    }
    NSMutableArray *ta =[db queryStock];
    [stockData addObjectsFromArray:ta];//重新添加
    if (stockData.count!=0){
        for (int i=0;i<ta.count;i++){
            StockCode *code =[ta objectAtIndex:i];
            [self updateStock:code.stockcode];
        }
    }else{
        [self nnn];
    }
}

//做一个for循环
-(void)updateStock:(NSString *)stockCode{//数据解析成功
    NSDictionary *params = @{@"gid":stockCode,@"key":@"0364782500bf476ffc1dc7557d18d5e5"};
    [HttpTool GET:@"http://web.juhe.cn:8080/finance/stock/hs" WithHeader:NO parameters:params success:^(id responseObject) {
        stockDetail *text =[stockDetail mj_objectWithKeyValues:responseObject];
        if ([text.reason isEqualToString:@"SUCCESSED!"]) {
        NSLog(@"resultcode＝＝＝＝%@",text.resultcode);
        NSArray *result=text.result;
        StockResult *ddd =[result objectAtIndex:0];
        StockData *data =ddd.data;
        NSLog(@"－－－－－－name====%@",data.name);
        [rightTableData addObject:data];
        
        if (rightTableData.count==stockData.count) {
            //重新排序
            NSMutableArray *order =[NSMutableArray array];
            for (int i=0; i<stockData.count;i++){//排序
                StockCode *code =[stockData objectAtIndex:i];
                for (int j=0; j<rightTableData.count; j++) {
                    StockData *data =[rightTableData objectAtIndex:j];
                    if ([code.name isEqualToString:data.name]) {
                        [order addObject:data];
                    }
                }
            }
            [rightTableData removeAllObjects];
            [rightTableData addObjectsFromArray:order];//重新排序之后
            [self performSelectorOnMainThread:@selector(nnn) withObject:nil waitUntilDone:NO];
        }
        }else{
            
        }
        } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
//请求数据真是久,赚个圈圈吧
-(void)nnn{
    NSLog(@"nnn");
    [tableView reloadData];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
     once =YES;
}
-(void)addStock{
    _add.userInteractionEnabled = YES;
    UITapGestureRecognizer *addTapGest = [[UITapGestureRecognizer alloc] init];
    addTapGest.numberOfTapsRequired = 1;
    addTapGest.delegate = self;
    [_add addGestureRecognizer:addTapGest];
    [addTapGest addTarget:self action:@selector(addTapView:)];
}
- (void)addTapView:(UITapGestureRecognizer *)addTapGest{
    StockAddVC *add =[[StockAddVC alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

-(void)editStock{
    _edit.userInteractionEnabled = YES;
    UITapGestureRecognizer *editTapGest = [[UITapGestureRecognizer alloc] init];
    editTapGest.numberOfTapsRequired = 1;
    editTapGest.delegate = self;
    [_edit addGestureRecognizer:editTapGest];
    [editTapGest addTarget:self action:@selector(editTapView:)];
}
- (void)editTapView:(UITapGestureRecognizer *)editTapGest{
    NSMutableArray *data =[NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        NSString *str = [NSString stringWithFormat:@"%d",i];
        [data addObject:str];
    }
    StockedEditVC *edit =[[StockedEditVC alloc]init];
    edit.data=stockData;
    [self.navigationController pushViewController:edit animated:YES];
}

- (void)initData {
    headData = [NSMutableArray array];
    [headData addObject:@"现价"];
    [headData addObject:@"涨幅"];
    [headData addObject:@"涨跌"];
    [headData addObject:@"最高"];
    [headData addObject:@"最低"];
    [headData addObject:@"今开"];
    [headData addObject:@"成交金额"];
    [headData addObject:@"成交量"];
    [headData addObject:@"昨开"];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - XCMultiTableViewDataSource


- (NSArray *)arrayDataForTopHeaderInTableView:(XCMultiTableView *)tableView {
    return headData;
}

//左边的数据
- (NSArray *)arrayDataForLeftHeaderInTableView:(XCMultiTableView *)tableView InSection:(NSUInteger)section {
    return stockData;
}

- (NSArray *)arrayDataForContentInTableView:(XCMultiTableView *)tableView InSection:(NSUInteger)section {
    return rightTableData;
}


- (NSUInteger)numberOfSectionsInTableView:(XCMultiTableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(XCMultiTableView *)tableView contentTableCellWidth:(NSUInteger)column {
    if (column == 0) {
        return SCREEN_W/4;
    }
    return SCREEN_W/4;
}

- (CGFloat)tableView:(XCMultiTableView *)tableView cellHeightInRow:(NSUInteger)row InSection:(NSUInteger)section {
    return 57.0f;
}

- (UIColor *)tableView:(XCMultiTableView *)tableView bgColorInSection:(NSUInteger)section InRow:(NSUInteger)row InColumn:(NSUInteger)column {
    if (row%2==0) {
        return [UIColor whiteColor];
    }else{
        return [UIColor colorwithHexString:@"#F4F3F8"];
    }
}

- (UIColor *)tableView:(XCMultiTableView *)tableView headerBgColorInColumn:(NSUInteger)column {
  return [UIColor colorwithHexString:@"#1186D6"];
}
@end
